package com.lancoo.klgcourseware.ui.fragment.usage;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.github.lzyzsd.circleprogress.DonutProgress;
import com.google.gson.Gson;
import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.HideWordModel;
import com.lancoo.klgcourseware.entity.WordPracticeModel;
import com.lancoo.klgcourseware.entity.bean.KlgConstant;
import com.lancoo.klgcourseware.entity.bean.KlgDbConstant;
import com.lancoo.klgcourseware.entity.bean.SentenceLineTrainBean;
import com.lancoo.klgcourseware.entity.bean.SentenceTrainBean;
import com.lancoo.klgcourseware.entity.bean.WordTrainBean;
import com.lancoo.klgcourseware.ui.adapter.SceneTrainAdapter;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.BasicTrainBaseFragment;
import com.lancoo.klgcourseware.ui.widget.CountTimeView;
import com.lancoo.klgcourseware.utils.KlgTimeUtils;
import com.lancoo.klgcourseware.utils.KlgTrainUtils;
import com.lancoo.klgcourseware.utils.klog.KLog;
import com.stkouyu.entity.EvaluateWord;
import com.stkouyu.entity.LgEvaluateObj;
import com.tt.LancooSkEgnManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengmi on 2019/8/24 19:15
 * Description:读句填空
 */
public class SceneFragment extends UsageSubBaseFragment {

    private static final String TAG = "UsageSubFragment";
    private final int DEFAULT_TRAIN_TIME=10000;//默认训练时长
    private final float RATE_SLOW=3f;
    private final float RATE_MIDDLE=2.5f;
    private final float RATE_FAST=2f;
    private final int TIME_WORD=3000;
    private final int TIME_UNWORD=5000;
    private SceneTrainAdapter sceneTrainAdapter;

    private int totalMusicPlayTime;//音频播放总时长
    private String evaluationSentence;//句子评测，单词评测
    private ImageView img_demonstrationSound,img_recorder,img_pause,img_complete;
    private TextView tv_endRecord,tv_repeatTime,tv_resultAlert,tv_readTime,tv_giveUp;
    private DonutProgress donutProgress;
    private RelativeLayout rl_recorder,rl_soundWave;
    private int recordDucation,usageScore,resultCountTime=3;//录音总时长，得分，下个流程3，2，1倒计时
    private CountTimeView countTimeView;

    private long mLastInputTime;
    private boolean mHasInput = false;//录音分贝检测辅助
    private List<SentenceLineTrainBean> sentenceLineTrainBeanList;

    @Override
    protected int getContentViewId() {
        return R.layout.klg_fragment_sub_usage_train;
    }

    @Override
    protected void initView(View view) {
        Log.e(TAG, "initView: "+quesIndex+","+quesTotal );
        KLog.json(new Gson().toJson(expressPracticeModel));
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        initEnglishContent();
        sceneTrainAdapter = new SceneTrainAdapter(getContext(),sentenceLineTrainBeanList);
        recyclerView.setAdapter(sceneTrainAdapter);

        view.findViewById(R.id.ll_demonstration_sound).setOnClickListener(this);
        img_demonstrationSound = view.findViewById(R.id.img_demonstration_sound);
        tv_endRecord = view.findViewById(R.id.tv_end_record);
//        tv_endRecord.setOnClickListener(this);
        donutProgress = view.findViewById(R.id.donut_progress);
        img_recorder = view.findViewById(R.id.img_recorder);
        img_recorder.setOnClickListener(this);
        img_pause = view.findViewById(R.id.img_pause);
        img_complete = view.findViewById(R.id.img_complete_logo);
        img_pause.setOnClickListener(this);
        tv_repeatTime = view.findViewById(R.id.tv_repeat_time);
        rl_recorder = view.findViewById(R.id.rl_recorder);
        countTimeView = view.findViewById(R.id.counttime_view);
        tv_resultAlert = view.findViewById(R.id.tv_spell_memory_write_alert);
        rl_soundWave = view.findViewById(R.id.rl_sound_wave);
        rl_soundWave = view.findViewById(R.id.rl_sound_wave);
        ImageView img_track01 = view.findViewById(R.id.img_track01);
        ImageView img_track02 = view.findViewById(R.id.img_track02);
        ImageView img_track03 = view.findViewById(R.id.img_track03);
        ImageView img_track04 = view.findViewById(R.id.img_track04);
        ImageView img_recorder = view.findViewById(R.id.img_recorder);
        volumeAnimationControl(img_track01,true);
        volumeAnimationControl(img_track02,true);
        volumeAnimationControl(img_track03,true);
        volumeAnimationControl(img_track04,true);
        tv_readTime=view.findViewById(R.id.tv_read_player_time);
        tv_giveUp=view.findViewById(R.id.tv_pass_train);
        tv_giveUp.setOnClickListener(this);
        tv_giveUp.setText(R.string.klg_train_again);
        view.findViewById(R.id.img_alert).setVisibility(getArguments().getBoolean(BasicTrainBaseFragment.KEY_IS_ALERT)?View.VISIBLE:View.GONE);
        resetUasgeTrainStyle();

    }

    @Override
    public void onClick(View v) {

        if(System.currentTimeMillis()-time<500){
            return;
        }
        time=System.currentTimeMillis();
        super.onClick(v);
        int id=v.getId();
        if(id==R.id.tv_end_record){
            img_recorder.setEnabled(false);
            tv_endRecord.setVisibility(View.GONE);
            stopSpeechEvaluation();
        }else if(id==R.id.ll_demonstration_sound){
            pauseSubTrain(false);
            playDemonstrationMusic();
        }else if(id==R.id.img_pause){
            pauseDemonstrationMusic();
            continueTrain();
        }else if(id==R.id.img_recorder){
            tv_endRecord.setVisibility(View.GONE);
            v.setEnabled(false);
            stopSpeechEvaluation();
        }else if(id==R.id.tv_pass_train){//再练一遍
            pauseDemonstrationMusic();
            resetUasgeTrainStyle();
            startTrainCountTime();
        }
    }

    private void initEnglishContent(){
        totalMusicPlayTime = 0;
        sentenceLineTrainBeanList = new ArrayList<>();
        int startIndex=1;
        StringBuilder trainSentence=new StringBuilder();
        for (WordPracticeModel wordPracticeModel:expressPracticeModel.getOneSentenceList()){
            String sentence = Html.fromHtml(wordPracticeModel.getSentence().replaceAll("<strong>","").replaceAll("</strong>","").replaceAll("</STRONG>","").replaceAll("<STRONG>","")).toString()+" rgb";
//            sentence= Html.fromHtml(sentence).toString().replaceAll("&\\*","&@&@&@&@&@&@&@&@&@&@");
            String[] sentenceSplit = sentence.split("&\\*");
            //构造训练全新数据结构
            SentenceLineTrainBean sentenceLineTrainBean=new SentenceLineTrainBean();
            //中文翻译
            sentenceLineTrainBean.setChineseSen(wordPracticeModel.getTranslate());
            List<SentenceTrainBean> sentenceTrainBeanList=new ArrayList<>();
            Log.e("aaaa","长度。。。"+sentenceSplit.length);
            for (int i = 0; i < sentenceSplit.length; i++) {
                if(i==sentenceSplit.length-1){
                    if(TextUtils.equals("rgb",sentenceSplit[i].trim())){
                        continue;
                    }else{
                        sentenceSplit[i]=sentenceSplit[i].substring(0,sentenceSplit[i].length()-4);
                    }
                }
                String sentenceOutFill = sentenceSplit[i];
                trainSentence.append(sentenceOutFill);
                if(TextUtils.isEmpty(sentenceOutFill.trim())){
                    continue;
                }
                SentenceTrainBean sentenceTrainBean=new SentenceTrainBean();
                sentenceTrainBean.setSentence(sentenceOutFill);
                sentenceTrainBean.setFill(false);
                setSentenceSplitToWord(sentenceOutFill,sentenceTrainBean,i==0);
                sentenceTrainBeanList.add(sentenceTrainBean);
                if(i!=sentenceSplit.length-1){
                    String sentenceFill=wordPracticeModel.getHideWordList().get(i).getWord();
                    trainSentence.append(sentenceFill);
                    SentenceTrainBean sentenceTrainBean2=new SentenceTrainBean();
                    sentenceTrainBean2.setSentence(sentenceFill);
                    sentenceTrainBean2.setFill(true);
                    sentenceTrainBean2.setFillIndex(startIndex);
                    sentenceTrainBeanList.add(sentenceTrainBean2);
                    startIndex++;
                    setSentenceSplitToWord(sentenceFill,sentenceTrainBean2,false);
                }
            }
            //训练内容
            sentenceLineTrainBean.setSentenceTrainBeanList(sentenceTrainBeanList);
            sentenceLineTrainBeanList.add(sentenceLineTrainBean);
            trainSentence.append(" ");
            totalMusicPlayTime +=wordPracticeModel.getIntVoiceTime();
        }
        Log.e(TAG,"totalMusicPlayTime:"+totalMusicPlayTime);
        totalMusicPlayTime=(totalMusicPlayTime==0?DEFAULT_TRAIN_TIME:totalMusicPlayTime);
        Log.e("aaaa","情景对话评测内容："+trainSentence.toString());
        KLog.json(JSON.toJSONString(sentenceLineTrainBeanList));
        evaluationSentence=trainSentence.toString();
    }

    private void setSentenceSplitToWord(String sentenceFill,SentenceTrainBean sentenceTrainBean,boolean isFirstSentence){
        String[] fillSpilt = sentenceFill.split(" ");
        List<WordTrainBean> wordTrainBeanList=new ArrayList<>();
        if(fillSpilt.length>1){
            for (int j = 0; j < fillSpilt.length; j++) {
                String word=fillSpilt[j];
                if(TextUtils.isEmpty(word.trim())){
                    continue;
                }
                WordTrainBean wordTrainBean=new WordTrainBean();
                wordTrainBean.setWord(word);
                wordTrainBeanList.add(wordTrainBean);
                if(isFirstSentence&&word.contains(":")){
                    wordTrainBean.setIgnoreTrain(true);
                }
            }
        }else{
            String word=sentenceFill;
            if(!TextUtils.isEmpty(word.trim())){
                WordTrainBean wordTrainBean=new WordTrainBean();
                wordTrainBean.setWord(word);
                if(isFirstSentence&&word.contains(":")){
                    wordTrainBean.setIgnoreTrain(true);
                }
                wordTrainBeanList.add(wordTrainBean);
            }

        }

        sentenceTrainBean.setWordTrainBeanList(wordTrainBeanList);
    }

    private void resetUasgeTrainStyle(){
        if(img_pause==null){
            return;
        }
        mHasInput=false;
        mLastInputTime=System.currentTimeMillis();
        img_pause.setVisibility(View.GONE);
        donutProgress.setProgress(0);
        img_recorder.setEnabled(true);
        rl_recorder.setVisibility(View.GONE);
        donutProgress.setVisibility(View.VISIBLE);
        tv_repeatTime.setVisibility(View.GONE);
        tv_endRecord.setVisibility(View.GONE);
        rl_soundWave.setVisibility(View.GONE);
        tv_giveUp.setVisibility(View.INVISIBLE);
        img_complete.setVisibility(View.GONE);
        countTimeView.setVisibility(View.VISIBLE);
        SharedPreferences sp=getContext().getSharedPreferences(KlgDbConstant.SP_KNOWLEDGE_COURSE, Context.MODE_PRIVATE);
        int speedState = sp.getInt(KlgDbConstant.SP_KEY_USAGE_SPEED, 0);
        recordDucation = (int)(totalMusicPlayTime*(3-0.5f*speedState));
        donutProgress.setMax(recordDucation);
        currentUsageState=UsageState.THREESECOND;
        sceneTrainAdapter.setTrainTimes(0);
        if(usageScore<70){
            tv_resultAlert.setText(R.string.klg_start_read_scene_fill_alert);
        }
        Log.e("aaaa","重置内容状态了。。。。即将开始例句朗读，请准备...");
        rl_soundWave.setVisibility(View.GONE);
        changeImageViewAnimationState(false);
        updateReadTime(0);
        audioAnimation(0);

    }

    private void showUsageTrainStyle(int alertId){
        if(rl_recorder==null){
            return;
        }
        switch (currentUsageState){
            case THREESECOND:
            case SENTENCERECORD:
                mHasInput=false;
                mLastInputTime=System.currentTimeMillis();
                currentUsageState=UsageState.SENTENCERECORD;
                tv_endRecord.setVisibility(View.VISIBLE);
                rl_recorder.setVisibility(View.VISIBLE);
                img_pause.setVisibility(View.GONE);
                donutProgress.setVisibility(View.VISIBLE);
                donutProgress.setProgress(0);
                img_recorder.setEnabled(true);
                tv_repeatTime.setVisibility(View.GONE);
                SharedPreferences sp=getContext().getSharedPreferences(KlgDbConstant.SP_KNOWLEDGE_COURSE, Context.MODE_PRIVATE);
                int speedState = sp.getInt(KlgDbConstant.SP_KEY_USAGE_SPEED, 1);
                recordDucation = (int)(totalMusicPlayTime*(3-0.5f*speedState));
                donutProgress.setMax(recordDucation);
                convertView.findViewById(R.id.rl_count_time).setVisibility(View.GONE);
                audioAnimation(0);
                break;
            case WORDRECORD:
                mHasInput=false;
                mLastInputTime=System.currentTimeMillis();
                rl_soundWave.setVisibility(View.GONE);
                tv_endRecord.setVisibility(View.VISIBLE);
                rl_recorder.setVisibility(View.VISIBLE);
                img_pause.setVisibility(View.GONE);
                donutProgress.setVisibility(View.VISIBLE);
                tv_repeatTime.setVisibility(View.GONE);
                donutProgress.setProgress(0);
                audioAnimation(0);
                img_recorder.setEnabled(true);
//                recordDucation=TextUtils.equals(getArguments().getString(BasicTrainBaseFragment.KEY_KLGTYPE),"C")?TIME_WORD:TIME_UNWORD;
                donutProgress.setMax(recordDucation);
                sceneTrainAdapter.setTrainTimes(0);
                break;
            case SENTENCERESULT:
                rl_recorder.setVisibility(View.GONE);
                resultCountTime=3;
                sceneTrainAdapter.setTrainTimes(3);
                tv_resultAlert.setText(R.string.klg_start_repeat_sentence_fill_alert);
                break;
            case WORDRESULT:
                rl_recorder.setVisibility(View.GONE);
                if(alertId!=0){
                    String message=String.format(getString(alertId),getString(getArguments().getInt(KEY_ALERT)));
                    tv_resultAlert.setText(message);
                }
                if(isQuality){
                    img_complete.setVisibility(View.VISIBLE);
                    countTimeView.setVisibility(View.GONE);
                }else{
                    img_complete.setVisibility(View.GONE);
                    countTimeView.setVisibility(View.VISIBLE);
                }
                resultCountTime=3;
//                tv_repeatTime.setVisibility(View.VISIBLE);
//                upDateRecordCountTime();
                sceneTrainAdapter.setTrainTimes(isQuality?1:2);
//                usageTrainAdapter.setTrainTimes(2);
                tv_giveUp.setVisibility(usageScore<60?View.VISIBLE:View.INVISIBLE);
                break;
            case REPEATMEDIAPLAY:
                Log.e("aaaaa","进到REPEATMEDIAPLAY来了");
                currentUsageState=UsageState.WORDRECORD;
                rl_soundWave.setVisibility(View.GONE);
                rl_recorder.setVisibility(View.VISIBLE);
                mHasInput=false;
                mLastInputTime=System.currentTimeMillis();
                donutProgress.setProgress(0);
                audioAnimation(0);
                donutProgress.setVisibility(View.VISIBLE);
//                tv_resultAlert.setVisibility(View.VISIBLE);
                tv_endRecord.setVisibility(View.VISIBLE);
                img_recorder.setEnabled(true);
                sceneTrainAdapter.setTrainTimes(0);
                break;
        }
    }



    /**
     * 开始语音评测
     */
    protected void startRecordTrain(){
        if(rl_recorder==null){
            return;
        }
        Log.e("aaaa","开始语音评测"+currentUsageState);
        showUsageTrainStyle(0);
//        startSpeechEvaluation("",currentUsageState==UsageState.SENTENCERECORD?evaluationSentence:evaluationWord);
        startSpeechEvaluation("",currentUsageState==UsageState.SENTENCERECORD?evaluationSentence:evaluationSentence);
    }


    protected void upDateRecordProgress(int progress){
        if(donutProgress==null){
            return;
        }

        donutProgress.setProgress(progress);
        if(progress>=recordDucation){
            tv_endRecord.setVisibility(View.GONE);
            stopSpeechEvaluation();
            mHasInput=false;
            return;
        }
        float volume = LancooSkEgnManager.getInstance(getContext()).getVolume();
        audioAnimation((int) volume);
        if (volume<2){
            if (mHasInput && System.currentTimeMillis() - mLastInputTime>2_200){
                //朗读录音停止了2s，结束录音
                tv_endRecord.setVisibility(View.GONE);
                stopSpeechEvaluation();
            }
        }else {
            mHasInput = true;
            mLastInputTime = System.currentTimeMillis();
        }
    }

    protected void onSuccessSpeechEvaluation(int grade, String url, LgEvaluateObj skegnResult){
        KLog.json(new Gson().toJson(skegnResult));
        List<EvaluateWord> evaluateWordList = skegnResult.getResult().getWords();
        int times=0;
        int index=0;
        int fillTotalScore=0;//填空处的作答总得分
        int fillTotalCount=0;//总的填空数
        for (SentenceLineTrainBean sentenceLineTrainBean:sentenceLineTrainBeanList){
            List<SentenceTrainBean> sentenceTrainBeanList = sentenceLineTrainBean.getSentenceTrainBeanList();
            for (SentenceTrainBean sentenceTrainBean:sentenceTrainBeanList){
                List<WordTrainBean> wordTrainBeanList = sentenceTrainBean.getWordTrainBeanList();
                int sencetenceScore=0;
                for (WordTrainBean wordTrainBean:wordTrainBeanList){
                    if(index<evaluateWordList.size()){
                        EvaluateWord evaluateWord = evaluateWordList.get(index);
                        Log.e("aaaaa",index+"评阅结果："+wordTrainBean.getWord()+"......"+evaluateWord.getWord()+
                                "....."+evaluateWord.getScores().getOverall());
                        wordTrainBean.setScore(evaluateWord.getScores().getOverall());
                        wordTrainBean.setQuality(evaluateWord.getScores().getOverall()>=KlgConstant.SCORE_QUALITY);
                        if(sentenceTrainBean.isFill()){
                            fillTotalScore+=evaluateWord.getScores().getOverall();
                            fillTotalCount++;
                        }
                        sencetenceScore+=evaluateWord.getScores().getOverall();
                    }
                    index++;
                }
                sentenceTrainBean.setSentenceScore(sencetenceScore/(wordTrainBeanList.size()==0?1:wordTrainBeanList.size()));
            }
        }
//        int fillAvgScore=fillTotalScore/(fillTotalCount==0?1:fillTotalCount);//填空处的平均分
//        usageScore=(int)(fillAvgScore*0.5+grade*0.5);
        int id=0;
        isQuality=grade>=KlgConstant.SCORE_QUALITY;
        if(isQuality){
            if(currentUsageState==UsageState.SENTENCERECORD){
                usageScore=100;
                id=R.string.klg_result_correct_read_sentence;
            }else{
                usageScore=85;
                id=R.string.klg_result_correct_repeat_sentence;
            }
            for (WordPracticeModel wordPracticeModel:expressPracticeModel.getOneSentenceList()){
                for (HideWordModel hideWordModel:wordPracticeModel.getHideWordList()){
                    hideWordModel.setGrade(85);
                }
            }
            currentUsageState=UsageState.WORDRESULT;
        }else {
            if(currentUsageState==UsageState.SENTENCERECORD){
                currentUsageState=UsageState.SENTENCERESULT;
                id=R.string.klg_start_repeat_sentence_fill_alert;
            }else{
                currentUsageState=UsageState.WORDRESULT;
                id=R.string.klg_result_error_repeat_sentence;
            }
        }

        playResultMusic(isQuality);
//        isQuality=true;
//        if( currentUsageState==UsageState.SENTENCERESULT){
//            startNextTrainCountTime();
//        }
        startNextTrainCountTime();
        showUsageTrainStyle(id);
    }

    protected int getScore(){
        Log.e("aaaa","得分。。。"+usageScore);
        return usageScore;
    }

    protected void pauseSubTrain(boolean isReset){
        if(isReset){
            resetUasgeTrainStyle();
        }else{
            Log.e("aaaaaa","暂停训练。。。"+currentUsageState);
            if(img_pause!=null){
                img_pause.setVisibility(View.VISIBLE);
                donutProgress.setProgress(0);
                audioAnimation(0);
                mHasInput=false;
                mLastInputTime=System.currentTimeMillis();
                img_recorder.setEnabled(true);
                donutProgress.setVisibility(View.INVISIBLE);
                rl_recorder.setVisibility(View.VISIBLE);
                rl_soundWave.setVisibility(View.GONE);
                tv_repeatTime.setVisibility(View.GONE);
                tv_endRecord.setVisibility(View.GONE);
                changeImageViewAnimationState(false);
                convertView.findViewById(R.id.rl_count_time).setVisibility(View.GONE);
            }
        }
    }

    /**
     * 继续训练
     */
    protected void continueTrain(){
        if(rl_recorder==null){
            return;
        }
        img_pause.setVisibility(View.GONE);
        switch (currentUsageState){
            case THREESECOND://3s倒计时
                rl_recorder.setVisibility(View.GONE);
                restartCountTimeTrain();
                Log.e("aaaa","3s倒计时");
                break;
            case SENTENCERECORD://朗读作答录音
                startDripMusic();
                Log.e("aaaa","朗读作答录音");
                break;
            case SENTENCERESULT://朗读2s倒计时
                showUsageTrainStyle(0);
                startNextTrainCountTime();
                Log.e("aaaa","朗读结果2s倒计时");
                break;
            case REPEATMEDIAPLAY://系统带读
                Log.e("aaaa","系统带读");
                updateReadTime(0);
                rl_recorder.setVisibility(View.GONE);
                rl_soundWave.setVisibility(View.VISIBLE);
                playDemonstrationMusic();
                break;
            case WORDRECORD://跟读作答
                startDripMusic();
                Log.e("aaaa","跟读作答");
                break;
            case WORDRESULT://跟读作答结果
                showUsageTrainStyle(0);
                startNextTrainCountTime();
                Log.e("aaaa","跟读作答结果");
                break;
        }
    }

    protected void changeImageViewAnimationState(boolean isPlaying){
        if(img_demonstrationSound==null){
            return;
        }
        if(isPlaying){
//                img.setImageResource(R.drawable.lgklg_animation_list_volume_cyan);
            img_demonstrationSound.setBackground(null);
            ((AnimationDrawable)img_demonstrationSound.getDrawable()).start();
        }else{
//                img.setImageResource(R.drawable.lgklg_volume_cyan_3);
            img_demonstrationSound.setBackgroundResource(R.drawable.klg_volume_cyan_3);
            ((AnimationDrawable)img_demonstrationSound.getDrawable()).stop();
            ((AnimationDrawable)img_demonstrationSound.getDrawable()).selectDrawable(0);

        }
    }

    protected void clearAnswerResult(){
        Log.e("aaaaa","作答数据清掉");
        usageScore=0;
        resetUasgeTrainStyle();
    }


    /**
     * 3s倒计时。。。
     * @param progress
     * @param totalProgress
     */
    protected void updateCountTime(int progress,int totalProgress){
//        Log.e("aaaaa","UsageSubFragment,"+progress);
        if(convertView==null){
            return;
        }
        if(convertView.findViewById(R.id.rl_count_time)==null){
            return;
        }
        currentUsageState=UsageState.THREESECOND;
        convertView.findViewById(R.id.rl_count_time).setVisibility(progress==totalProgress?View.GONE:View.VISIBLE);
        countTimeView.setAngle(progress,totalProgress);
    }

    /**
     * 朗读或是跟读结果状态样式
     * @param progress
     * @param totalProgress
     */
    protected void updateResultCountTime(int progress,int totalProgress){
        if(convertView==null){
            return;
        }
        if(convertView.findViewById(R.id.rl_count_time)==null){
            return;
        }
        convertView.findViewById(R.id.rl_count_time).setVisibility(progress==totalProgress?View.GONE:View.VISIBLE);
        countTimeView.setAngle(progress,totalProgress);
        if(progress==totalProgress){
            if(currentUsageState==UsageState.SENTENCERESULT){
                //开始系统带读
                showUsageTrainStyle(0);
                currentUsageState=UsageState.REPEATMEDIAPLAY;
                rl_soundWave.setVisibility(View.VISIBLE);
                playDemonstrationMusic();
//                sceneTrainAdapter.setTrainTimes(0);
//                showUsageTrainStyle();
//                startDripMusic();
            }else {
                showUsageTrainStyle(0);
//                resetUasgeTrainStyle();
                currentUsageState=UsageState.WORDRESULT;
                startNextQuesTraion();
                time=System.currentTimeMillis();//禁止点击界面其他按钮
                Log.e(TAG, "upDateSecondCountime: startNextQuesTraion" );
            }
            return;
        }
    }

    protected void volumeAnimationControl(ImageView img, boolean isAnimation){
        if(img!=null&&img.getVisibility()==View.VISIBLE){
            if(isAnimation){
//                img.setImageResource(R.drawable.lgklg_animation_list_volume_cyan);
                img.setBackground(null);
                ((AnimationDrawable)img.getDrawable()).start();
            }else{
//                img.setImageResource(R.drawable.lgklg_volume_cyan_3);
                img.setBackgroundResource(R.drawable.klg_volume_cyan_3);
                ((AnimationDrawable)img.getDrawable()).stop();
                ((AnimationDrawable)img.getDrawable()).selectDrawable(0);
            }
        }
    }

    private void updateReadTime(int time){
        Log.e("aaaaa","updateReadTime:"+time);
        if(getContext()==null){
            return;
        }
        if(tv_readTime!=null){
            String readTime=String.format(getString(R.string.klg_sentence_play_time), KlgTimeUtils.getTimeMinAndSecond(time));
            tv_readTime.setText(readTime);
        }
    }

    protected void updateMediaPlayerProgress(int progress){
        updateReadTime(progress/1000);
    }

    private void audioAnimation(int db){
        try {
            ImageView imgRecorder = convertView.findViewById(R.id.img_recorder);
            LayerDrawable drawable =(LayerDrawable) imgRecorder.getDrawable();
            Drawable clipDrawable = drawable.getDrawable(1);

            clipDrawable.setLevel(KlgTrainUtils.getAudioDBLevel(db));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
